"""
姓名:张三,李四,王五,小明,小红
成绩:张三,语文(100),10;李四,语文(30),1;王五,语文(60),4;小明,语文(66),20;小红,语文(99),3
要求：提取成绩，并按成绩降序排列
输出到文件的格式：
科目 姓名 成绩 编号 是否及格
语文  张三 1oo 10  是
语文  小红 99  3   是
"""


class Student:
    sub = "语文"

    def __init__(self, name, score, num):
        self.name = name
        self.score = score
        self.num = num
        if int(score) >= 60:
            self.aa = "是"
        else:
            self.aa = "否"


# 读取
def read_stu_score():
    with open("q1.txt", "r", encoding="utf-8") as text:
        text.readline()
        return text.readline()


# 组合学生成绩,并排序
def make_data(text):
    tx = text.split(":")[1].split("\n")
    lists = tx[0].split(";")
    stu_list = []
    for i in lists:
        b = i.split(",")
        sc = (b[1])[3:len(b[1]) - 1]
        stu = Student(b[0], int(sc), b[2])
        stu_list.append(stu)

    def sort_by_score(stu: Student):
        return stu.score

    # return stu_list   #不排序
    # return sorted(stu_list, key=sort_by_score, reverse=True)  #方式1
    return sorted(stu_list, key=lambda st: st.score, reverse=True)  # 方式2


# 输出学生成绩到文件
def out_file(stu_list: list[Student]):
    sub = "语文"
    tt = open("q3.txt", "w", encoding="utf-8")
    rline = "科目\t姓名\t分数\t编号\n"
    tt.writelines(rline)
    for stu in stu_list:
        rline = "" + sub + "\t" + stu.name + "\t" + str(stu.score) + "\t" + stu.num + "\n"
        tt.writelines(rline)
    tt.close()


if __name__ == '__main__':
    stu_scores = read_stu_score()
    stu_list = make_data(stu_scores)
    out_file(stu_list)
